import 'package:bruno/bruno.dart';
import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:miaowu/app/common/window_pop/public_btn_tem.dart';
import 'package:miaowu/app/common/window_pop/public_btn_widget.dart';

class PrivacySettingsPage extends StatefulWidget {
  const PrivacySettingsPage({super.key});

  @override
  State<PrivacySettingsPage> createState() => _PrivacySettingsPageState();
}

class _PrivacySettingsPageState extends State<PrivacySettingsPage> {
  bool access = false;
  bool ipValue = false;
  bool giftValue = false;

  List<PublicBtnItem>? list = [];

  PublicBtnItem? sethideview;
  PublicBtnItem? sethideipaddr;
  PublicBtnItem? sethidegiftwall;

  @override
  void initState() {
    fetchgPrivacySetConfig();
    super.initState();
  }

  void fetchgPrivacySetConfig() {
    HttpProvider.get('/user/getPrivacySet', explain: '隐私设置页', success: (result) {
      if (result.data["data"] is List) {
        list = result.data["data"] == null
            ? []
            : (result.data["data"] as List).map((e) => PublicBtnItem.fromJson(e)).toList();
      }
      for (PublicBtnItem element in list ?? []) {
        if (element.key == 'sethideview') {
          sethideview = element;
          // BtnClickParams? params = element.click?.params;
          // access = (params?.value ?? '0').toInt() == 1;
        }
        if (element.key == 'sethideipaddr') {
          sethideipaddr = element;
          // BtnClickParams? params = element.click?.params;
          // ipValue = (params?.value ?? '0').toInt() == 1;
        }
        if (element.key == 'sethidegiftwall') {
          sethidegiftwall = element;
          // BtnClickParams? params = element.click?.params;
          // giftValue = (params?.value ?? '0').toInt() == 1;
        }
      }
      setState(() {});
    });
  }

  void fetchSubmitBaseSet({required String key, required String value, Function? callback}) {
    HttpProvider.post(
      '/user/subBaseSet',
      params: {'setKey': key, 'setVal': value},
      explain: '修改设置',
      success: (result) {
        callback?.call();
      },
      failure: (result) {
        Toast.showText(result.message);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BrnAppBar(title: '隐私设置', backgroundColor: Colors.white),
        backgroundColor: const Color(0xFFF6F6F6),
        body: list == null
            ? const SizedBox()
            : Column(
                children: [
                  if (sethideview != null && sethideview!.view == 1)
                    _buildItem(
                        item: sethideview!,
                        value: access,
                        action: (value) {
                          publicCheckClickAction(
                            btnItem: sethideview!,
                            alertCallBack: (item) {},
                            clickAction: (btnItem) {
                              fetchSubmitBaseSet(
                                  key: sethideview!.click!.option!,
                                  value: value,
                                  callback: () {
                                    access = value;
                                    setState(() {});
                                  });
                            },
                          );
                        }),
                  _buildItem(item: PublicBtnItem(), value: true),
                  if (sethideipaddr != null && sethideipaddr!.view == 1)
                    _buildItem(
                        item: sethideipaddr!,
                        value: ipValue,
                        isShowLine: true,
                        action: (value) {
                          publicCheckClickAction(
                            btnItem: sethideipaddr!,
                            alertCallBack: (item) {},
                            clickAction: (btnItem) {
                              ipValue = value;
                              setState(() {});
                              fetchSubmitBaseSet(
                                  key: sethideview!.click!.option!,
                                  value: value,
                                  callback: () {
                                    access = value;
                                    setState(() {});
                                  });
                            },
                          );
                        }),
                  if (sethidegiftwall != null && sethidegiftwall!.view == 1)
                    _buildItem(
                        item: sethidegiftwall!,
                        value: giftValue,
                        action: (value) {
                          publicCheckClickAction(
                            btnItem: sethidegiftwall!,
                            alertCallBack: (item) {},
                            clickAction: (btnItem) {
                              fetchSubmitBaseSet(
                                  key: sethideview!.click?.option ?? '',
                                  value: value.toString(),
                                  callback: () {
                                    giftValue = value;
                                    setState(() {});
                                  });
                            },
                          );
                        }),
                ],
              ));
  }

  Widget _buildItem({
    // required String title,
    required bool value,
    required PublicBtnItem item,
    Function? action,
    bool isShowLine = false,
  }) {
    // BtnClickParams? params = item.click?.params;
    // int value = (params?.value ?? '0').toInt();
    Display? display = item.display;
    String subtitle = display?.subtitle ?? '';
    return Container(
      color: (item.title ?? '').isNotEmpty ? Colors.white : Colors.transparent,
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: (item.title ?? '').isNotEmpty
          ? Column(
              children: [
                Container(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Text('${item.title}  ',
                                  style: TextStyle(fontSize: 15.sp, color: Colors.black, fontWeight: FontWeight.w500)),
                              Image.asset('assets/app/images/mine/vip_icon.png', width: 44),
                            ],
                          ),
                          if (subtitle.isNotEmpty) const SizedBox(height: 5),
                          if (subtitle.isNotEmpty)
                            Text(subtitle,
                                maxLines: 2,
                                style: TextStyle(
                                    fontSize: 12.sp, color: const Color(0xFF999999), fontWeight: FontWeight.w500)),
                        ],
                      ),
                      const Spacer(),
                      BrnSwitchButton(
                        value: value,
                        onChanged: (value) {
                          action?.call(value);
                        },
                      )
                    ],
                  ),
                ),
                if (isShowLine) const Divider(color: Color(0xFFEEEEEE), height: 0.5, indent: 15, endIndent: 15)
              ],
            )
          : const SizedBox(height: 10),
    );
  }
}
